<?php
class Search extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->model('blog_model');
		$this->load->model('user_model');
		$this->load->model('tag_model');
		$this->load->model('like_model');
		$this->load->model('feed_model');
		$this->load->model('reply_model');
		$this->load->model('follow_model');
		$this->load->model('search_model');
		$this->load->library('datelib');
	}
	
	
	public function index(){
		$keywords = $this->input->get('q');
		$type = $this->input->get('t');
		if ($type == ''){
			$type = 'feed';
		}
		
		$cur_uid = $this->session->userdata('user_id');
		
		$data = array(
				'main_url'=>'index.php/search/area?q='.$keywords.'&t='.$type,
				'js_load'=>array(
				),
		);
		if ($cur_uid){
			$data['user_info'] = $this->user_model->get_user($cur_uid);
		}
		
		$this->load->view('template/header',$data);
		
		$this->load->view('framework/main',$data);
		$this->set_post();
		$this->load->view('template/footer');
	}
	
	public function area(){
		$keywords = $this->input->get('q');
		$reasult['keywords'] = $keywords;
		$kw_arr = explode(' ', $keywords);
		$reasult['kw_arr'] = $kw_arr;
		$type = $this->input->get('t');
		$reasult['type'] = $type;
		if ($type=='blog'){
			$reasult['items'] = $this->search_model->search_blog($kw_arr);
		} elseif($type=='user') {
			$reasult['items'] = $this->search_model->search_user($kw_arr);
		} else {
			$feeds = $this->search_model->search_tag($kw_arr);
			$feeds = $data['feeds'] = $this->attach_detail($feeds);
			$reasult['items'] = $feeds;
		}
		$this->load->view('search/result',$reasult);
	}
	
	private function attach_detail(&$posts){
		$uid = $this->session->userdata('user_id');
		foreach ($posts as &$feed){
			$aid = $feed['author'];
			$bid = $feed['bid'];
			$feed['author'] = $this->user_model->get_user($aid);
			$cur_user = $this->user_model->get_user($uid);
			if ($feed['author']['uname']==$cur_user['uname']){
				$feed['author']['uname'] = '<strong>you</strong>';
			}
			$blog = $this->blog_model->get_blog($bid);
			$feed['blog_name'] = $blog['bname'];
			$feed['tags'] = $this->tag_model->get_tags_array($feed['pid']);
			$feed['islike'] =$this->like_model->islike($uid,$feed['pid']);
			$feed['num_likes'] =$this->like_model->likes_num($feed['pid']);
			$feed['num_reply'] = $this->reply_model->get_num_reply($feed['pid']);
			$feed['num_share'] = $this->feed_model->get_share_num($feed['pid']);
			if($feed['type']=='share'){
				$feed['orig'] = $this->feed_model->get($feed['content']);
				if($feed['orig']){
					$orig_arr = array($feed['orig']);
					$orig_arr = $this->attach_detail($orig_arr);
					$feed['orig'] = $orig_arr[0];
				} else {
					$feed['orig']['type'] = 'delete';
				}
			}
		}
		return $posts;
	}
	
	private function set_post() {
		$bid = $this->session->userdata('blog_id');
		$binfo = $this->blog_model->get_blog($bid);
		$blogdata['blog_name'] = $binfo['bname'];
		$data['blog_name'] = $binfo['bname'];
		$this->load->view('feed/post',$blogdata);
	}
	
}

?>